using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Read.Models;
using Read.Services;
using Read.Utility;

namespace Read.Web.Admin
{
    /// <summary>
    /// 角色管理
    /// </summary>
    public class RoleController : AdminBaseController
    {
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(QueryParamsRole query)
        {
            if (HttpContext.Request.IsAjaxRequest())
            {
                var revice = new RoleService();
                var list = revice.getRoleList(query);
                var res = new ResultData<List<RoleModel>>()
                {
                    Result = true,
                    Total = list.Count,
                    Data = list
                };
                return Json(res);
            }
            else
            {
                ViewBag.Title = "角色列表";
                return View();
            }
        }

        /// <summary>
        /// 角色授权
        /// </summary>
        /// <returns></returns>
        public IActionResult Authorization(int roleId, string nodes)
        {
            var revice = new RoleService();
            var auth = revice.authorization(roleId, nodes);
            var res = new ResultData<bool>()
            {
                Result = true,
                Data = auth,
                Message = "角色授权成功"
            };
            return ResultJSON(res.Result, res, res.Message);
        }

        /// <summary>
        /// 角色添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(RoleModel model)
        {
            var revice = new RoleService();
            var add = revice.roleAdd(model);
            var res = new ResultData<long>()
            {
                Result = true,
                Data = add,
                Message = "添加角色成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }

        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Edit(RoleModel model)
        {
            var revice = new RoleService();
            var edit = revice.roleEdit(model);
            var res = new ResultData<bool>()
            {
                Result = true,
                Data = edit,
                Message = "编辑角色成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int id)
        {
            var srevice = new RoleService();
            var edit = srevice.roleDel(id);
            var res = new ResultData<bool>()
            {
                Result = true,
                Data = edit,
                Message = "删除角色成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }
    }
}